﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Text;


namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JSUserDWFApplyList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "JSUserDWFApplyList";
            }
        }        
        protected string GetFolderTree(int ParFolderID)
        {
            List<JSDWFFolderInfo> list = JSDWFFolderController.GetAllByParFolderId(ParFolderID);
            if (list.Count <= 0)
                return "";

            /// <ul><li><a href="JSDWF/JSDWFFolderPageList.aspx?ParFolderID=1" target="ajax" rel="jbsxBox">尿检</a></li></ul>
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul>");
            foreach (JSDWFFolderInfo itm in list)
            {
                sb.Append("<li>");
                sb.AppendFormat("<a href=\"JSDWF/JSUserDWFApplyFolderList.aspx?FolderID={0}\" target=\"ajax\" rel=\"JSUserDWFApplyListDWFjbsxBox\">{1}</a>", itm.FolderID, itm.FolderName);

                sb.Append(GetFolderTree(itm.FolderID));

                sb.Append("</li>");
            }
            sb.Append("</ul>");

            return sb.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "apply")
            {
                addApply();
            }

            if (GetRequestValue<string>("ajaxmethod", "") == "getAdtlvlinfo")
            {
                Response.Write(   getAdtlvlinfo());
                Response.End();
                
            }

        }
        /// <summary>
        /// 添加申请
        /// </summary>
        private void addApply()
        {
            string dwfids = GetRequestValue<string>("dwfid", "");
            string remark = GetRequestValue<string>("remark", "");
            int AdtlvlID = GetRequestValue<int>("AdtlvlID", 0);
            int r = JSDWFApplyController.Add(User.UserName, AdtlvlID,remark, dwfids);
            if (r > 0)
                SuccessJson("已成功提交您的申请,审批结果将会以邮件的方式通知您，请注意查收您的邮件");
            else
                ErrorJson("提交失败,请联系IT部");
        }

        private string getAdtlvlinfo()
        {
            int AdtlvlID = GetRequestValue<int>("AdtlvlID", 0);
            SAuditLevelInfo aduobj = SAuditLevelController.GetAditlvlById(SAuditLevelType.DWF, AdtlvlID);
            List<JSDWFFlowConfigInfo> list = JSDWFFlowConfigController.GetAll(AdtlvlID);
            string html = "您申请使用的图纸最高级别为[<b>{0}</b>].<br/>将会提交到 {1} 进行审批.";
            string usernamelist = "";
            foreach(JSDWFFlowConfigInfo itm in list)
            {
                OUserInfo obj = OUserController.GetByUserName(itm.UserName);
                usernamelist += string.Format("<b>{0}</b>&nbsp;", obj.RealName);
            }

            return string.Format(html, aduobj.Title, usernamelist);
        }

    }
}